var _max_code_length = 5;

$(document).ready(init_keypad);

function init_keypad() {

	var clickEvent = 'touchstart';
	var releaseEvent = 'touchend';

	var isIPhone = /CPU iPhone/.exec(navigator.userAgent);
	if (!isIPhone) {
		clickEvent = 'click';
	}
	
	$('#keypad .keypad-button').live(clickEvent, function() {

		if (isIPhone) {
			$(this).css({
				'-webkit-transition-property': '-webkit-transform',
				'-webkit-transition-duration': '40ms',
				'-webkit-transition-easing': 'ease-in',
				'-webkit-transform': 'translateY(4px)',
			});
		}
		
		button_clicked(this);
	});
	
	if (isIPhone) {
		
		$('#keypad .keypad-button').live(releaseEvent, function() {
			$(this).css({
				'-webkit-transition-property': '-webkit-transform',
				'-webkit-transition-duration': '40ms',
				'-webkit-transition-easing': 'ease-out',
				'-webkit-transform': 'translateY(0px)',
			});
		});
	}
}

function button_clicked(button) {

	var label = $('#keypad .code-label');
	
	// Check for clear button
	if (button.id == 'button-clear') {
		label.text('');
		return;
	}
	
	if (label.text().length >= _max_code_length) {
		return;
	} else {
		label.text(label.text() + button.id.split('-')[1]);
	}
}
